package ar.com.pragmatia.lanacion.data;

import java.util.Vector;

import android.content.ContentValues;
import ar.com.pragmatia.lanacion.app.App;
import ar.com.pragmatia.lanacion.utils.Strings;

public class Visit extends AppData implements Datable {

	public static final String TABLE_NAME = "visits";
	public static final String ID = "AUTOINCREMENT";
	public static final String FIELDS[] = { "id", "seller_id", "client_id",
			"contact", "reason", "result", "claim", "items_delivered",
			"observations", "date", "next_visit_date", "created", "modified",
			"uuid", "sent", "location" };

	private String sellerId;
	private String clientId;
	private String contact;
	private String visitDate;
	private String reason;
	private String result;
	private String claim;
	private String itemsDelivered;
	private String observations;
	private String nextVisitDate;
	private String created;
	private String modified;
	private String uuid;
	private String sent;
	private String location;

	public Visit(String id, String clientId, String contact, String visitDate,
			String reason, String result, String claim, String itemsDelivered,
			String observation, String nextVisitDate) {
		super();
		this.clientId = clientId;
		this.contact = contact;
		this.visitDate = visitDate;
		this.reason = reason;
		this.result = result;
		this.claim = claim;
		this.itemsDelivered = itemsDelivered;
		this.observations = observation;
		this.nextVisitDate = nextVisitDate;
	}

	public Visit() {
		sent = "false";
	}

	public String getLocation() {
		return location;
	}

	public void setLocation(String location) {
		this.location = location;
	}

	public String getUuid() {
		return uuid;
	}

	public String getSellerId() {
		return sellerId;
	}

	public void setSellerId(String sellerId) {
		this.sellerId = sellerId;
	}

	public Client getClient() {
		Client client = null;
		if (getClientId() != null || !getClientId().equals("")) {
			client = (Client) (new Client().find("WHERE id = '" + getClientId()
					+ "'").get(0));
		}
		return client;
	}

	public String getObservations() {
		return observations;
	}

	public void setObservations(String observations) {
		this.observations = observations;
	}

	public String getSent() {
		return sent;
	}

	public void setSent(String sent) {
		this.sent = sent;
	}

	public void setUuid(String uuid) {
		this.uuid = uuid;
	}

	public String getCreated() {
		return created;
	}

	public void setCreated(String created) {
		this.created = created;
	}

	public String getModified() {
		return modified;
	}

	public void setModified(String modified) {
		this.modified = modified;
	}

	public String getClientId() {
		return clientId;
	}

	public void setClientId(String clientId) {
		this.clientId = clientId;
	}

	public String getContact() {
		return contact;
	}

	public void setContact(String contact) {
		this.contact = contact;
	}

	public String getVisitDate() {
		return visitDate;
	}

	public void setVisitDate(String visitDate) {
		this.visitDate = visitDate;
	}

	public String getDate() {
		return visitDate;
	}

	public void setDate(String visitDate) {
		this.visitDate = visitDate;
	}

	public String getReason() {
		return reason;
	}

	public void setReason(String reason) {
		this.reason = reason;
	}

	public String getResult() {
		return result;
	}

	public void setResult(String result) {
		this.result = result;
	}

	public String getClaim() {
		return claim;
	}

	public void setClaim(String claim) {
		this.claim = claim;
	}

	public String getItemsDelivered() {
		return itemsDelivered;
	}

	public void setItemsDelivered(String itemsDelivered) {
		this.itemsDelivered = itemsDelivered;
	}

	public String getObservation() {
		return observations;
	}

	public void setObservation(String observation) {
		this.observations = observation;
	}

	public String getNextVisitDate() {
		return nextVisitDate;
	}

	public void setNextVisitDate(String nextVisitDate) {
		this.nextVisitDate = nextVisitDate;
	}

	public boolean save(boolean flag) {

		if (flag) {
			setId(getInsertId());
			setSellerId(App.getUser().getId());
			ContentValues values = new ContentValues();

			for (int i = 0; i < FIELDS.length; i++) {

				String value = null;
				try {
					value = executeStringGetterMethod(this.getClass(), "get"
							+ Strings.camelize(FIELDS[i], false));
				} catch (NoSuchMethodException e) {
					// e.printStackTrace();
					// App.log("AppData", "save", this.getClass().getName());
					App.log("AppData", "save", e.getMessage());
				}

				if (FIELDS[i].equals("id") && Strings.isNullOrEmpty(value)) {
					continue;
				} else {
					value = (Strings.isNullOrEmpty(value)) ? "" : value;
					values.put(FIELDS[i], value);
				}

			}

			long r = -1;
			try {
				r = App.getDb().insert(getTableName(), null, values);
			} catch (Exception e) {
				e.printStackTrace();
				App.log("AppData", "save", e.getMessage());
			}
			return (r >= 0) ? true : false;
		}
		return false;
	}

	private String getInsertId() {
		String id = "10000";
		Vector<AppData> v = this.find("WHERE sent = 'false' ORDER BY id DESC");
		if (v.size() > 0) {
			id = (new Integer(v.get(0).getId()) + 1) + "";
		}
		return id;
	}

	@Override
	public String toString() {
		return "Visit [id=" + getId() + ", clientId=" + clientId + ", contact="
				+ contact + ", visitDate=" + visitDate + ", reason=" + reason
				+ ", result=" + result + ", claim=" + claim
				+ ", itemsDelivered=" + itemsDelivered + ", observation="
				+ observations + ", nextVisitDate=" + nextVisitDate
				+ ", created=" + created + ", modified=" + modified + ", Uuid="
				+ uuid + ", sent=" + sent + "]";
	}

}
